package cn.elane.kibana.demo.global.render;

public class R {

    private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
    private static final String DEFAULT_ERROR_MESSAGE = "ERROR";
    private static final Boolean DEFAULT_SUCCESS_BOOLEAN = true;
    private static final Boolean DEFAULT_FAIL_BOOLEAN = false;

    private int code;
    private Boolean success;
    private String msg;
    private Object data;
    private long timestamp = System.currentTimeMillis();

    /**
     * 私有构造
     */
    private R() {
    }

    public static R success() {
        return new R()
                .setCode(ResultCode.SUCCESS)
                .setSuccess(DEFAULT_SUCCESS_BOOLEAN)
                .setMsg(DEFAULT_SUCCESS_MESSAGE)
                .setData("");
    }

    public static R success(String msg) {
        return new R()
                .setCode(ResultCode.SUCCESS)
                .setSuccess(DEFAULT_SUCCESS_BOOLEAN)
                .setMsg(msg)
                .setData("");
    }

    public static R success(Object data) {
        return new R()
                .setCode(ResultCode.SUCCESS)
                .setSuccess(DEFAULT_SUCCESS_BOOLEAN)
                .setMsg(DEFAULT_SUCCESS_MESSAGE)
                .setData(data);
    }

    public static R success(String msg, Object data) {
        return new R()
                .setCode(ResultCode.SUCCESS)
                .setSuccess(DEFAULT_SUCCESS_BOOLEAN)
                .setMsg(msg)
                .setData(data);
    }

    public static R fail() {
        return new R()
                .setCode(ResultCode.FAIL)
                .setSuccess(DEFAULT_FAIL_BOOLEAN)
                .setMsg(DEFAULT_ERROR_MESSAGE)
                .setData("");
    }

    public static R fail(String msg) {
        return new R()
                .setCode(ResultCode.FAIL)
                .setSuccess(DEFAULT_FAIL_BOOLEAN)
                .setMsg(msg)
                .setData("");
    }

    public static R fail(ResultCode resultCode) {
        return new R()
                .setCode(resultCode)
                .setSuccess(DEFAULT_FAIL_BOOLEAN)
                .setMsg(DEFAULT_ERROR_MESSAGE)
                .setData("");
    }

    public static R fail(String msg, ResultCode resultCode) {
        return new R()
                .setCode(resultCode.code)
                .setSuccess(DEFAULT_FAIL_BOOLEAN)
                .setMsg(msg)
                .setData("");
    }

    public R setCode(ResultCode resultCode) {
        this.code = resultCode.code;
        return this;
    }

    public int getCode() {
        return code;
    }

    public R setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public R setData(Object data) {
        this.data = data;
        return this;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public Boolean getSuccess() {
        return success;
    }

    public R setSuccess(Boolean success) {
        this.success = success;
        return this;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}
